﻿using System.Text;
using System.Web;
using SilenthinkContract.Domain;
using System.Collections.Generic;
using SilenthinkContract.Component;

namespace Web.Manage
{
    public static class TreeHelper
    {
        public static IHtmlString GenerateProductCategoryTree(IList<TreeNode<ProductCategory>> tree)
        {
            var sb = new StringBuilder();
            if (tree.Count > 0)
            {
                foreach (var node in tree)
                {
                    var cls = string.Empty;
                    if (node.Parent != null)
                    {
                        cls = "child-of-node-" + node.Parent.Data.Code;
                    }
                    sb.AppendFormat("<tr id=\"node-{0}\" target=\"param\" rel=\"{1}\" class=\"{2}\" >", node.Data.Code, node.Data.Id, cls);
                    sb.AppendFormat("<td>{0}</td>", node.Data.Name);
                    sb.AppendFormat("<td>{0} 级分类</td>", node.Data.Level);
                    sb.AppendFormat("<td>{0}</td>", node.Parent != null ? node.Parent.Data.Name + "-" + node.Data.Name : node.Data.Name);
                    if (node.Children != null && !node.Data.HasChildren)
                    {
                        sb.Append("<td>");
                        sb.AppendFormat("<span><a onclick='void(0);' mask=\"true\" href='/ProductAttributeName/?productCategoryCode={0}' target=\"dialog\" rel=\"/ProductAttributeName/\" title=\"{1} - 属性\">{1} - 属性</a><span>", node.Data.Code, node.Data.Name);
                        sb.AppendFormat("<span><a onclick='void(0);' mask=\"true\" href='/Product/Create/?productCategoryCode={0}' target=\"dialog\" rel=\"/Product/Create/\" width=\"840\" height=\"520\" title=\"{1} - 添加商品\">添加商品</a><span>", node.Data.Code, node.Data.Name);
                        sb.Append("</td>");
                    }
                    else
                    {
                        sb.Append("<td>");
                        sb.AppendFormat("<span><a onclick='void(0);' mask=\"true\" href='/ProductAttributeName/?productCategoryCode={0}' target=\"dialog\" rel=\"/ProductAttributeName/\" title=\"{1} - 公共属性\">{1} - 公共属性</a><span>", node.Data.Code.Substring(0, 2), node.Data.Name);
                        sb.Append("</td>");
                    }
                    sb.AppendFormat("</tr>");
                    if (node.Children != null && node.Data.HasChildren)
                    {
                        sb.Append(GenerateProductCategoryTree(node.Children));
                    }
                }
            }
            return new HtmlString(sb.ToString());
        }
    }
}